<?php
declare(strict_types=1);

namespace App\Resource\Dynamic\Essay;


use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Services\Tools\RedisService;

class Resource extends JsonResource
{
    public function toArray($request)
    {
//        print_r($this->user);die;
        Carbon::setLocale('zh');
        return [
            'id' => $this->id,
            'user_id' => $this->user_id,
            'topic_id' => $this->topic_id,
            'is_like' => (empty($this->like) || empty($this->like->count())) ? 0 : 1,
            'is_follow' => (empty($this->user->followed) || empty($this->user->followed->count())) ? 0 : 1,
            'like_count' => $this->like_count,
            'follow_count' => $this->follow_count,
            'browse_count' => $this->browse_count,
            'reply_count' => $this->reply_count,
            'share_count' => $this->share_count,
            'status' => $this->status,
            'score' => $this->score,
            'context' => \App\Resource\Dynamic\Context\Resource::collection($this->context),
            'media' => \App\Resource\Dynamic\Media\Resource::collection($this->media),
            'user' => [
                'id' => $this->user->user_id ?? 0,
                'code' => $this->user->user_data->user_code ?? 0,
                'nickname' => $this->group_nickname ?? $this->user->user_data->nickname ?? '',
                'sex' => $this->user->user_data->sex ?? 0,
                'age' => $this->user->user_data->birthday ? birthToAge($this->user->user_data->birthday, true) : 0,
                'head_picture' => $this->user->user_data->head_picture ?? '',
                'level_icon' => $this->user->grade->head_picture?? '',
                'charm_icon' =>  app('model_charm_config')->getCharmIcon($this->user->charm_id ?? 0),
                'nobility_icon' =>  app('model_nobility')->getNobilityIcon($this->user->nobility ?? 0),
                'decorate' => (!isset($this->user->decorate) || empty($this->user->decorate)) ? (object)[] : $this->user->decorate,
                'recharge_icon' => RedisService::getUserRechargeIcon($this->user->user_id),
            ],

            'show_at' => Carbon::make($this->created_at)->diffForHumans(),
            'timestamp' => Carbon::make($this->created_at)->timestamp,
            'created_at' => Carbon::make($this->created_at)->toDateTimeString(),
        ];
    }
}
